import { defineConfig } from 'vite'
import vue from '@vitejs/plugin-vue'
import path from 'path'
import { viteMockServe } from 'vite-plugin-mock'

//svg
import viteSvgIcons from 'vite-plugin-svg-icons'


// https://vitejs.dev/config/
export default ({ mode }) => {
  const MODE = mode;

  return defineConfig({
    base: './',
    resolve: {
      alias: {
        '@': path.resolve(__dirname, "src"),
        'comps': path.resolve(__dirname, "src/components"),
        'api': path.resolve(__dirname, "src/api"),
        'styles': path.resolve(__dirname, "src/styles"),
        'source': path.resolve(__dirname, "src/assets"),
        'store': path.resolve(__dirname, "src/store"),
        'configs': path.resolve(__dirname, "src/configs"),
        'util': path.resolve(__dirname, "src/util"),
      },
    },
    server: {
      host: '0.0.0.0',
      port: 3000,
      strictPort: true,
      // open: true,
      proxy: {
        '/api': {
          target: MODE === 'development' ? 'http://0.0.0.0:8080/' : 'http://0.0.0.0:8080/', // 后台接口域名
          changeOrigin: true, //是否跨域
          ws: true, //如果要代理 websockets，配置这个参数
          rewrite:  path => path.replace(/^\/api/, '')
        }
      },
    },
    // 打包相关规则
    build: {
      rollupOptions: {
        output: {
          //解决打包时Some chunks are larger警告
          manualChunks(id) {
            if (id.includes('node_modules')) {
              return id.toString().split('node_modules/')[1].split('/')[0].toString();
            }
          }
        }
      }
    },
    plugins: [
      vue(),
      viteMockServe({
        mockPath: 'mock',
        logger: true,
        localEnabled: MODE === 'development' ? true : false,
        prodEnabled: false
      }),
      viteSvgIcons({
        // Specify the icon folder to be cached
        iconDirs: [path.resolve(process.cwd(), 'src/icons')],
        // Specify symbolId format
        symbolId: 'icon-[dir]-[name]'
      })
    ],
    css: {
      preprocessorOptions: {
        scss: {
        },
      }
    }
  })
}
